package priv.dajie.tzzq.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import priv.dajie.tzzq.Base;

import java.io.IOException;

/**
 * @ClassName ProxyPool
 * @Description
 * @Author dajie
 * @Date 2019/10/8 4:53 下午
 * @Version V1.0
 **/
public class ProxyPoolXs implements ProxyPool{

    private Base base;
    private static ProxyPoolXs proxyPoolXs = null;
//    private HttpGet proxyHttpGet = new HttpGet("http://www.xsdaili.com/get?orderid=197465732326122&num=1&area=%E4%B8%AD%E5%9B%BD%20%E5%B9%BF%E4%B8%9C&carrier=1&xsdq=1&r_xylx=1&r_nmd=1&r_yys=1&sp1=1&dedup=1&format=json&gj=1");
    private HttpGet proxyHttpGet = new HttpGet("http://www.xsdaili.com/get?orderid=197465732326122&num=1&area=%E4%B8%AD%E5%9B%BD%20%E5%B9%BF%E4%B8%9C&xsdq=1&r_xylx=1&r_nmd=1&r_yys=1&sp1=1&format=json&gj=1");

    private HttpGet testHttpGet = new HttpGet("https://buff.163.com");
    private Logger logger = LogManager.getLogger(ProxyPoolXs.class);

    private ProxyPoolXs() {
        base = new Base();
    }

    public static ProxyPoolXs getInstance() {
        if (proxyPoolXs == null) {
            synchronized (ProxyPoolXs.class) {
                if (proxyPoolXs == null) {
                    proxyPoolXs = new ProxyPoolXs();
                }
            }
        }
        return proxyPoolXs;
    }

    @Override
    public synchronized Proxy getProxy() {
        for (int i = 0; i < 10; i++) {
            String jsonStr = base.open(proxyHttpGet);
            JSONObject json = JSON.parseObject(jsonStr);
            JSONArray data = json.getJSONArray("data");
            if (data == null) {
//                logger.error(json);
            } else {
//                logger.info(json);
                for (int j = 0; j < data.size(); j++) {
                    String item = data.getString(j);
                    String[] info = item.split(",");
                    String[] ipAndPort = info[0].split(":");
                    String ip = ipAndPort[0];
                    int port = Integer.parseInt(ipAndPort[1]);
                    ProxyType proxyType = "HTTPS".equals(info[1]) ? ProxyType.HTTPS : ProxyType.HTTP;
                    Proxy proxy = new Proxy(ip, port, proxyType);
                    HttpResponse httpResponse = null;
                    try {
                        httpResponse = base.openPro(testHttpGet, true, proxy);
                        if (httpResponse.getStatusLine().getStatusCode() != 200) {
                            continue;
                        }
                        return proxy;
                    } catch (Exception e) {
//                        e.printStackTrace();
                    } finally {
                        if (httpResponse != null) {
                            try {
                                httpResponse.getEntity().getContent().close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        return null;
    }


}
